package date;

import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;

public class LocalDateTimeTest {
    public static void main(String[] args) {

        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDateTime);
        LocalDateTime localDateTime1 = LocalDateTime.of(2025, 1, 1, 1, 1, 1);
        System.out.println(localDateTime1);
        //加减
        System.out.println(localDateTime1.plusMonths(1).minusYears(1));
        //年份
        System.out.println(localDateTime.getYear());
        //月份
        System.out.println(localDateTime.getMonth().getValue());
        // 时间戳
        System.out.println(Instant.now());
        // 时间戳转毫秒
        System.out.println(Instant.now().toEpochMilli());
        //计算两个LocalDateTime对象之间的时间差，并以天数形式输出。
        Duration between = Duration.between(localDateTime, localDateTime1);
        System.out.println(between.toDays());

        //格式化
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy.MM.dd HH:mm:ss");
        String format = dateTimeFormatter.format(localDateTime);
        System.out.println(format);
        TemporalAccessor parse = dateTimeFormatter.parse("2025.01.01 01:01:01");
        System.out.println(parse);
    }
}
